<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
    <title>商店详情</title>
    <link rel="stylesheet" href="css/weui.min.css">
    <link rel="stylesheet" href="css/jquery-weui.css">
    <link rel="stylesheet" href="css/demos.css">
    <link rel="stylesheet" type="text/css" href="./js/swiper.min.css"/>
    <link rel="stylesheet" href="css/main.css">

</head>
<style>
    body {
        background: #fff;
    }

    .pay_img {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        margin-right: 10px;
    }

    .pay_top {
        display: flex;
        align-items: center;
        padding: 20px;
        background: #fff;
    }


    .pay_btn {
        width: 300px;
        border-radius: 5px;
        background: #6670ff;
        text-align: center;
        color: #fff;
        font-size: 18px;
        margin: 20px auto 0;
        height: 40px;
        line-height: 40px;
    }

    .pay_group {
        margin: 10px 20px;
        padding: 10px;
        box-shadow: 0 0 3px #dcdcdc;
    }

    .pay_item {
        margin: 20px 0;
        display: flex;
        align-items: flex-end;
        color: #666;
        border-bottom: 1px solid #dcdcdc;
    }

    .pay_item input {
        border: 0;
        outline: none;
        flex: 1;
        height: 30px;
        font-size: 16px;
    }

    .pay_item span {
        display: inline-block;
        min-width: 80px;
        height: 30px;
        line-height: 30px;
    }

    .yhj {
        justify-content: space-between;
        border: 0;
    }

    input::-webkit-input-placeholder {
        color: #A9A9A9;
        font-size: 14px;
    }

    input::-moz-placeholder {
        /* Mozilla Firefox 19+ */
        color: #A9A9A9;
        font-size: 14px;
    }

    input:-moz-placeholder {
        /* Mozilla Firefox 4 to 18 */
        font-size: 14px;
        color: #A9A9A9;
    }

    input:-ms-input-placeholder {
        /* Internet Explorer 10-11 */
        font-size: 14px;
        color: #A9A9A9;
    }

    .moneyc {
        font-size: 18px;
    }

    .divdisable {
        pointer-events: none;
    }
</style>

<body ontouchstart>
<div class="topNav">
    <a href="javascript:void(0);" onclick="window.history.go(-1);"><img src="./images/xiangzuo.png" alt=""></a>
    <p>返回</p>
</div>
<div class="line"></div>
<div class="block"></div>
<div class="pay_top">
    <img src="./images/head_img.png" alt="" class="pay_img">
    <p>昵称</p>
    <p id="shopName">(*商家)</p>
</div>
<div class="pay_group">
    <div class="pay_item">
        <span class="moneyc">￥</span>
        <input type="number" name="beforePrice" >
    </div>
    <div class="pay_item">
        <span>使用积分</span>
        <input type="number" placeholder="" name="useJifen" id="useJifen">
    </div>
    <a href="javascript:void(0);" onclick="goCoupon()">
        <div class="pay_item yhj">
            <span>使用优惠券</span>
            <i class="iconfont icon-you"></i>
            <i id="couponName" hidden></i>
        </div>
    </a>
</div>
<div class="pay_btn">确认支付</div>

<div class="model">
    <div class="model-content">
        <iframe src="" frameborder="0"></iframe>
        <div class="model-footer">
            <div class="quit" id="quit">取消</div>
        </div>
    </div>
</div>
</body>
<script src="js/jquery-2.1.4.js"></script>
<script src="js/jquery-weui.js"></script>
<script src="js/swipe.js"></script>
<script src="js/swiper.min.js" type="text/javascript" charset="utf-8"></script>
<script src="wxJs/utils/storage.js"></script>
<script src="wxJs/utils/contain.js"></script>
<script>
    var userId = GetQueryString("userId")
    var shopId = GetQueryString("shopId");   //商铺id
    var shopName = GetUrlByParamName("shopName");   //商铺名称
    var beforePrice = ""
    var userInfo

    if (!shopId || !userId) {
        window.history.back(-1);
    }else {
        if (userId!=0){
            userInfo = getUserInfo(userId)
            $("#shopName").text("("+shopName+")")
            $("#useJifen").attr("placeholder","当前积分"+userInfo.jiFen+"(10积分 = 1元)")
        } else {
            window.location.href = "me.html?userId="+userId
        }
    }
    var couponId = "";     //使用优惠券
    //iframe隐藏
    $('.model').hide()

    /*打开优惠券页面*/
    function goCoupon() {
        beforePrice = $('input[name="beforePrice"]').val()
        if (!beforePrice) {    //输入金额不能空
            $.toast("请输入金额", "forbidden")
            return false;
        }
        $(".model iframe").prop('src', 'use_yhq.html?beforePrice=' + beforePrice+"&userId="+userId)
        $('.model').show()
    }

    /*子页面使用优惠券传递父页面的参数*/
    function toCoupon(cid, couponName) {
        couponId = cid
        $('.model').hide();
        $(".yhj i").hide();
        $('#couponName').text(couponName).show()
    }

    /*监听价格的输入*/
    $('input[name="beforePrice"]').blur(function () {
        couponId = ""
        $(".yhj i").show();
        $('#couponName').text("").hide()
    })


    //iframe取消
    $('#quit').on('click', function () {
        $('.model').hide()
    });
    /*确认支付*/
    $('.pay_btn').on("click", function () {

        var formData = new FormData();
        var beforePrice = $('input[name="beforePrice"]').val()
        var useJifen = $('input[name="useJifen"]').val()

        if (Number(beforePrice)>0){

        }else {
            $.toast("请输入正确的金额", "forbidden")
            return false;
        }
        formData.append("beforePrice", beforePrice)

        if (Number(useJifen)>0) {     //积分兑换
            if (useJifen > userInfo.jiFen) {
                $.toast("可用积分不得超过" + userInfo.jiFen, "forbidden")
                return false;
            } else {
                if (parseFloat((useJifen / 10).toFixed(2)) >= parseFloat(beforePrice)) {     //当积分的钱大于支付金额提示无法支付
                    $.toast("积分换算的钱不得超过金额", "forbidden")
                    return false;
                }
                formData.append("useJifen", useJifen)
            }
        }

        if (couponId) { //使用优惠券
            formData.append("couponId", couponId);
        }
        formData.append("wxUserId", userInfo.id);
        formData.append("openId", userInfo.openId);
        formData.append("shopId", shopId)

        $(this).addClass("divdisable");
        $.ajax({
            url: getWeiContextPath() + "/wx/order/toPay",
            type: "POST",
            data: formData,
            cache: false,        // 不缓存数据
            processData: false,  // 不处理数据
            contentType: false,   // 不设置内容类型
            success: function (res) {
                console.log(res)//成功回调
                if (res.code == 0) {
                    doWxpay(res.data)
                } else {
                    $.toast(res.msg, "forbidden")
                }

            }
        });

    })


    function doWxpay(data) {
        if (data.code == 0) {
            $('#msg').fadeIn(100, function () {
                $.toast(data.msg, 'forbidden', 2000);
            })
        } else {
            //支付
            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady',
                        jsApiCall(data), false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady',
                        jsApiCall(data));
                    document.attachEvent('onWeixinJSBridgeReady',
                        jsApiCall(data));
                }
            } else {
                jsApiCall(data);
            }
        }
    }

    function jsApiCall(data) {
        WeixinJSBridge.invoke('getBrandWCPayRequest', {
            "appId": data.appId, //公众号名称
            "timeStamp": data.timeStamp, //时间戳，自1970年以来的秒数
            "nonceStr": data.nonceStr, //随机串
            "package": data.package,
            "signType": data.signType, //微信签名方式
            "paySign": data.paySign
        }, function (res) {
            if (res.err_msg == "get_brand_wcpay_request:ok") { //支付成功
                $.toast("支付成功", 2000);
                $.ajax({
                    type: "POST",
                    cache: false,
                    data: {userId: userInfo.id},
                    async: false,
                    url: getWeiContextPath() + "/wx/VipUser/queryUser",
                    success: function (result) {
                        window.location.href = 'store.html?shopId=' + shopId+"&userId="+userId;
                    },
                    error: function (result) {
                        alert("网络走丢了")
                    }
                });

            } else if (res.err_msg == "get_brand_wcpay_request:cancel") { //支付取消
                $('.pay_btn').removeClass('divdisable')
            } else if (res.err_msg == "get_brand_wcpay_request:fail") { //支付失败
                alert(JSON.stringify(res));
                $('.pay_btn').removeClass('divdisable')
            }
        });
    }


    function GetUrlByParamName(name)
    {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var URL =  decodeURI(window.location.search);
        var r = URL.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    };

</script>
</html>